﻿/*******************************
 * Class responsible for the behavior of the application. It holds the settings and is responsible 
 * for saving the different objects to xml, in order to be able to retrieve those data when re starting
 * the application. 
 *****************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using DlhSoft.Windows.Data;
using UTC_Project_Manager.Model.Infrastructure;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using DlhSoft.Windows.Controls;
using System.Windows;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Windows.Forms;




namespace UTC_Project_Manager.Model
{
    public class Config
    {
        public Project OurProject { get; private set; }
        public TaskManager taskManager;
        public BindingList<UTC_Project_Manager.Model.Infrastructure.TaskItem> taskItems;
        //public ObservableCollection<TaskItem> taskItems;
        public ObservableCollection<ResourceItem> assignableResourceItems;
        public Dictionary<DateTime, string> dict = new Dictionary<DateTime, string>();
        public String SavePath { get; set; }
        public String LoadPath { get; set; }
        public bool IsLoaded { get; set; }

        public Config()
        {
            if (load() == false)
            {
                // configuration file not found => creates a project and instantiates its properties
                OurProject = new Project();
                if (OurProject.Members == null) OurProject.Members = new List<Member>();
                if (OurProject.Tasks == null) OurProject.Tasks = new List<ProjectTask>();
                if (OurProject.Notes == null) OurProject.Notes = new List<Note>();
                if (OurProject.Meetings == null) OurProject.Meetings = new List<Meeting>();
                IsLoaded = false;
            }
            else IsLoaded = true;
            
        }

        // Unserialize an Xml configuration file to a Project object
        public Boolean load()
        {
            XmlSerializer s = new XmlSerializer(typeof(Project));
            try
            {
                TextReader r = new StreamReader("ProjectManagerConfiguration.xml");
                OurProject = (Project)s.Deserialize(r);
                r.Close();
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.Message, "Error loading the XML file", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        public Boolean save()
        {
            try
            {
                XmlSerializer s = new XmlSerializer(typeof(Project));
                TextWriter w = new StreamWriter("ProjectManagerConfiguration.xml");
                s.Serialize(w, OurProject);
                w.Close();
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.Message, "Error saving the XML file", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        public void loadTasks()
        {
            // Prepare the assignable resource data collection.
            assignableResourceItems = new ObservableCollection<ResourceItem>();
            if (OurProject.Members != null) {
                foreach (Member member in OurProject.Members)
                {
                    assignableResourceItems.Add(new ResourceItem { Content = member.ToString() });
                };
            }

            // Populate OurProject.Tasks
            //initalizeProjectTask();

            // Pupulate the ObservableCollection taskItems
            initializeListViewTasks();

            // Prepare the assignable resource manager.
            ResourceManager assignableResourceManager = new ResourceManager(assignableResourceItems);

            // Prepare the task manager.
            taskManager = new TaskManager(taskItems, assignableResourceManager, Schedule.Standard);
        }

        // Temporal function to populate ProjectTasks List
        public void initalizeProjectTask() { 
            OurProject.Tasks = new List<ProjectTask>();

            OurProject.Tasks.Add(new ProjectTask { Content = "Tâche 1", Start = DateTime.Parse("26/05/2009 08:00"), Finish = DateTime.Parse("29/05/2009 12:15"), Effort = TimeSpan.Parse("23:00:00"), Duration = TimeSpan.Parse("23:00:00"), IsExpanded = true, Completion = 52.17, CompletedEffort = TimeSpan.Parse("12:00:00"), IndexString = "1" });
            OurProject.Tasks.Add(new ProjectTask { Content = "Tâche 1.1", Indentation = 1, Start = DateTime.Parse("26/05/2009 08:00"), Finish = DateTime.Parse("27/05/2009 14:00"), Effort = TimeSpan.Parse("14:00:00"), Duration = TimeSpan.Parse("14:00:00"), Completion = 50, CompletedEffort = TimeSpan.Parse("07:00:00"), IndexString = "2" });
            OurProject.Tasks.Add(new ProjectTask { Content = "Tâche 1.2", Indentation = 1, Start = DateTime.Parse("28/05/2009 11:15"), Finish = DateTime.Parse("29/05/2009 12:15"), Effort = TimeSpan.Parse("09:00:00"), Duration = TimeSpan.Parse("09:00:00"), Completion = 55.56, CompletedEffort = TimeSpan.Parse("05:00:00"), PredecessorsString = "2", IndexString = "3" });
            OurProject.Tasks.Add(new ProjectTask { Content = "Tâche 2", Start = DateTime.Parse("01/06/2009 15:00"), IsMilestone = true, PredecessorsString = "1", IndexString = "4" });

        }

        // OurProject.Task is initialized when we load the xml!
        // For now we are going to use a function called initializeProjectTasks()
        // Prepare the task data collection.
        public void initializeListViewTasks() {

            //taskItems = new ObservableCollection<TaskItem>();
            taskItems = new BindingList<UTC_Project_Manager.Model.Infrastructure.TaskItem>();
            foreach (ProjectTask pTask in OurProject.Tasks) { 
                UTC_Project_Manager.Model.Infrastructure.TaskItem taskItem = new UTC_Project_Manager.Model.Infrastructure.TaskItem { 
                    Content = pTask.Content,
                    IndexString = pTask.IndexString,
                    Indentation = pTask.Indentation,
                    
                    Start = pTask.Start,
                    Finish = pTask.Finish,

                    Duration = pTask.Duration,
                    Effort = pTask.Effort,
                    CompletedEffort = pTask.CompletedEffort,
                    Completion = pTask.Completion,
                    
                    IsCompleted = pTask.IsCompleted,
                    IsExpanded = pTask.IsExpanded,
                    IsMilestone = pTask.IsMilestone,

                    // For now, only one member by task
                    AssignmentsString = pTask.AssignmentsString,
                    PredecessorsString = pTask.PredecessorsString
                };

                // Add the taskItem to the ObservableCollection
                taskItems.Add(taskItem);
            }
        }
    }
}
